package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.model.BaseCategoryTrademark;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.atguigu.gmall.product.model.CategoryTrademarkVo;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 分类品牌中间表 业务实现类
 *
 * @author muzhuo
 * @since 2023-07-25
 */
@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    @Autowired
    private BaseTrademarkService baseTrademarkService;

    @Override
    public List<BaseTrademark> getTrademarkList(Long category3Id) {

        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        List<BaseCategoryTrademark> list = this.list(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            List<Long> tradeMarkIdList = list.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            //2.根据品牌ID集合查询品牌集合
            return baseTrademarkService.listByIds(tradeMarkIdList);
        }

        return null;

    }

    @Override
    public List<BaseTrademark> getCurrentTrademarkList(Long category3Id) {
        //获取持久层mapper 调用mapper自定义方法 编写动态SQL
        return this.getBaseMapper().getCurrentTrademarkList(category3Id);
    }

    @Override
    public void saveBaseCategoryTrademark(CategoryTrademarkVo vo) {
        List<Long> trademarkIdList = vo.getTrademarkIdList();
        if(!CollectionUtils.isEmpty(trademarkIdList)){
            List<BaseCategoryTrademark> list = trademarkIdList.stream().map(tradeMarkId -> {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setTrademarkId(tradeMarkId);
                baseCategoryTrademark.setCategory3Id(vo.getCategory3Id());
                return baseCategoryTrademark;

            }).collect(Collectors.toList());
            this.saveBatch(list);
        }

    }

    @Override
    public void removeCategoryTrademark(Long category3Id, Long trademarkId) {
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        queryWrapper.eq(BaseCategoryTrademark::getTrademarkId, trademarkId);
        this.remove(queryWrapper);

    }
}
